package com.ww.idiom.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.ww.core.activity.MyActivity;
import com.ww.core.util.CoreSharedPreferencesHelper;
import com.ww.core.widget.slidemenu.SlidingMenu;
import com.ww.idiom.R;
import com.ww.idiom.adapter.IdiomAdapter;
import com.ww.idiom.db.IdiomDbHelper;
/**
 * @version 2013-7-11
 * @author wangwei
 */
public class ResultActivity extends MyActivity  {
	private ArrayList<String> list = new ArrayList<String>();
	private ViewPager pager;
	private TextView num,title;
	private ImageButton back;
	protected static SlidingMenu mSlidingMenu;
	public static ResultActivity instance = null;
	Activity context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_result);
		context=ResultActivity.this;
		findView();
		getData();

		pager.setAdapter(new IdiomAdapter(context, list));
		pager.setOnPageChangeListener(new GuidePageChangeListener());
		if (list.size() > 0) {
			int t = context.getIntent().getIntExtra("index", -1);
			if (t != -1) {
				pager.setCurrentItem(t);
				setTitle(t, list.get(t));
			} else {
				CoreSharedPreferencesHelper h = new CoreSharedPreferencesHelper(context);
				String str = h.getValue("index");
				if (str != null) {
					pager.setCurrentItem(Integer.parseInt(str));
					setTitle(Integer.parseInt(str), list.get(Integer.parseInt(str)));
				} else {
					pager.setCurrentItem(0);
					setTitle(0, list.get(0));
				}
			}
		}
	}
	

	private void getData() {
		if (context.getIntent().getStringArrayListExtra("list") != null) {
			list =context. getIntent().getStringArrayListExtra("list");
			back.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					context.finish();
				}
			});
		} else {
			IdiomDbHelper s = new IdiomDbHelper(context);
			list = s.getWordList();
		}
	}

	private void findView() {
		back = (ImageButton) findViewById(R.id.back);
		num = (TextView) findViewById(R.id.num);
		pager = (ViewPager) findViewById(R.id.pager);
		title= (TextView) findViewById(R.id.title);
	}

	private final class GuidePageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {
			setTitle(arg0, list.get(arg0));
			int tt = 0;
			CoreSharedPreferencesHelper h = new CoreSharedPreferencesHelper(context);
			String str = h.getValue("index");
			if (str != null) {
				tt = Integer.parseInt(str);
			}
			if (arg0 > tt) {
				h.setValue("index", arg0 + "");
			}
		}
	}

	public void setTitle(int index, String txt) {
		num.setText((index + 1) + "/" + list.size());
		title.setText(txt);
	}
	
	
}
